(function () {
    "use strict";

    app.config(configBlocks)
        .run(runBlocks);

    function configBlocks($compileProvider, $urlRouterProvider, $controllerProvider, $filterProvider, $provide, $translateProvider) {
        $compileProvider.debugInfoEnabled(false);
        $urlRouterProvider.otherwise(function ($injector) {
            $injector.get("$state").go("app.dashboard", {}, {reload: true});
        });

        // lazy controller, directive and service
        app.controller = $controllerProvider.register;
        app.directive = $compileProvider.directive;
        app.filter = $filterProvider.register;
        app.factory = $provide.factory;
        app.service = $provide.service;
        app.constant = $provide.constant;
        app.value = $provide.value;

        // register a loader for the static files
        // so, the module will search missing translation tables under the specified urls.
        // those urls are [prefix][langKey][suffix].
        $translateProvider.useStaticFilesLoader({
            prefix: "l10n/",
            suffix: ".js"
        });
        $translateProvider.preferredLanguage("zh_CN"); // tell the module what language to use by default
        $translateProvider.useLocalStorage();          // tell the module to store the language in the local storage
    }

    function runBlocks($rootScope, $state, $translate, AuthService, RESTService, $http) {
        $rootScope.$state = $state;
        $rootScope.locale = "zh_CN" === $translate.proposedLanguage() ? "Cn" : "En";
        $rootScope.moment = window.moment;
        $rootScope.isSubmit = false;
        $rootScope.access_token = RESTService.getAccess_Token();
        //console.log('access_token:',$rootScope.access_token);
        // for modify patient
        $rootScope.redirectToDicomViewer = function (caseid) {
            $rootScope.implOpenDicomViewer(caseid, true, true, true);
        };

        // for specialist
        $rootScope.openDicomViewer4Specialist = function (caseid) {
            $rootScope.implOpenDicomViewer(caseid, true, false, true);
        };

        // for query
        $rootScope.openDicomViewer4Query = function (caseid) {
            $rootScope.implOpenDicomViewer(caseid, true, false, true);
        };


        $rootScope.implOpenDicomViewer = function (caseid, canView, canUpload, canUseNormalTools, canUseProprietaryTools) {
            var lang = "English" === $rootScope.selectLang ? "En" : "Cn";
            var url_dicomviewer = "http://121.43.148.35/dicomviewer/app/#";
            var newWin = window.open("", "_blank");

            var isContractedDoctor = false;
            var isPaidDoctor = false;
            if ($rootScope.currentUser != null) {
                if ($rootScope.currentUser.isMedicalAdmin) {
                    isContractedDoctor = true;
                    isPaidDoctor = true;
                }
                else if ($rootScope.currentUser._doctor != null) {
                    isContractedDoctor = $rootScope.currentUser._doctor.isContractedDoctor;
                    isPaidDoctor = $rootScope.currentUser._doctor.isPaidDoctor;
                }
            }
            // get random string
            // n : length of random string
            function generateMixed(n) {
                var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
                var res = "";
                for (var i = 0; i < n; i++) {
                    var id = Math.ceil(Math.random() * 61);
                    res += chars[id];
                }
                return res;
            }

            var rdm = generateMixed(20);
            // console.log(rdm);
            $rootScope.access_token = RESTService.getAccess_Token();
            if (isContractedDoctor == true || isPaidDoctor == true) {
                canUseProprietaryTools = true;
            } else {
                canUseProprietaryTools = false;
            }
            ;
            // var url=AuthService.getDsContext() + "/rest/permission/setPermission?access_token=" + $rootScope.access_token
            //     + "&id=" + caseid + "&canView=" + canView + "&canUpload=" + canUpload
            //     + "&canUseNormalTools=" + canUseNormalTools + "&canUseProprietaryTools=" + canUseProprietaryTools;
            //console.log($rootScope.currentUser._doctor.isContractedDoctor,'---',$rootScope.currentUser._doctor.isPaidDoctor);
            $http.post(AuthService.getDsContext() + "/rest/permission/setPermission?access_token=" + $rootScope.access_token
                + "&id=" + caseid + "&canView=" + canView + "&canUpload=" + canUpload
                + "&canUseNormalTools=" + canUseNormalTools + "&canUseProprietaryTools=" + canUseProprietaryTools + "&access_randomkey=" + rdm)
                .then(function (response) {
                        if (response.data.code == 0) {
                            //console.log('success');
                            newWin.location.href =
                                url_dicomviewer
                                + '/dicom/case?'
                                + 'access_token=' + $rootScope.access_token
                                + "&access_randomkey=" + rdm
                                + '&id=' + caseid
                                + '&lang=' + lang
                                + '&returnUrl=' + window.location.href.replace("#", "$");
                        } else {
                            //console.log('error');
                            newWin.location.href.replace("#", "_blank");
                        }
                    }
                );

        }

    }

})();